"""Configuration settings for AI service."""

from typing import Optional, Dict, Any
from pydantic_settings import BaseSettings
from pydantic import Field


class Settings(BaseSettings):
    """Application settings."""
    
    # Application
    app_name: str = Field(default="ai-service", env="APP_NAME")
    app_env: str = Field(default="development", env="APP_ENV")
    debug: bool = Field(default=False, env="DEBUG")
    log_level: str = Field(default="INFO", env="LOG_LEVEL")
    
    # API
    api_host: str = Field(default="0.0.0.0", env="API_HOST")
    api_port: int = Field(default=8001, env="API_PORT")
    api_prefix: str = Field(default="/api/v1", env="API_PREFIX")
    
    # MongoDB
    mongodb_url: str = Field(default="mongodb://localhost:27017", env="MONGODB_URL")
    mongodb_database: str = Field(default="ai_writing", env="MONGODB_DATABASE")
    
    # Redis
    redis_url: str = Field(default="redis://localhost:6379", env="REDIS_URL")
    redis_prefix: str = Field(default="ai_service:", env="REDIS_PREFIX")
    
    # Model API Keys
    openai_api_key: Optional[str] = Field(default=None, env="OPENAI_API_KEY")
    anthropic_api_key: Optional[str] = Field(default=None, env="ANTHROPIC_API_KEY")
    baidu_api_key: Optional[str] = Field(default=None, env="BAIDU_API_KEY")
    baidu_secret_key: Optional[str] = Field(default=None, env="BAIDU_SECRET_KEY")
    alibaba_api_key: Optional[str] = Field(default=None, env="ALIBABA_API_KEY")
    deepseek_api_key: Optional[str] = Field(default=None, env="DEEPSEEK_API_KEY")
    deepseek_base_url: str = Field(default="https://api.deepseek.com", env="DEEPSEEK_BASE_URL")
    
    # Model configuration
    default_model: str = Field(default="deepseek-chat", env="DEFAULT_MODEL")
    default_temperature: float = Field(default=0.7, env="DEFAULT_TEMPERATURE")
    default_max_tokens: int = Field(default=2000, env="DEFAULT_MAX_TOKENS")
    
    # Monitoring
    prometheus_port: int = Field(default=9091, env="PROMETHEUS_PORT")
    metrics_enabled: bool = Field(default=True, env="METRICS_ENABLED")
    
    # Security (for API key encryption)
    MASTER_KEY: Optional[str] = Field(default=None, env="MASTER_KEY")
    
    @property 
    def MONGODB_URL(self) -> str:
        """MongoDB URL property for compatibility."""
        return self.mongodb_url
    
    @property
    def database_name(self) -> str:
        """Database name property."""
        return self.mongodb_database
    
    class Config:
        env_file = ".env"
        case_sensitive = False


# Global settings instance
settings = Settings()


def get_settings() -> Settings:
    """Get settings instance.
    
    Returns:
        Settings instance
    """
    return settings